package com.whfc.ms.api.fse.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.DateUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.entity.dto.warn.AppWarnRuleDTO;
import com.whfc.entity.param.warn.AppWarnHandleParam;
import com.whfc.fse.dto.FseCraneWarnCountDTO;
import com.whfc.fse.dto.FseWarnDetailDTO;
import com.whfc.fse.dto.FseWarnRecordDTO;
import com.whfc.fse.param.FseWarnSetUserParam;
import com.whfc.fse.param.WarnBatchDelParam;
import com.whfc.fse.param.WarnBatchHandleParam;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.fse.service.MsFseWarnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @DESCRIPTION
 * @AUTHOR GuoDong_Sun
 * @DATE 2020/4/13
 */
@RestController
@RequestMapping("/ms/api/fse/warn")
public class FseWarnController extends BaseController {
    @Autowired
    private MsFseWarnService msFseWarnService;


    @GetMapping("/rule/list")
    public Result ruleList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestParam("deptId") Integer deptId,
                           @RequestParam(value = "type", required = false) Integer type) {
        logger.info("特种设备的报警规则列表,loginer:{},deptId:{},type:{}", user.getUsername(), deptId, type);
        ListData<AppWarnRuleDTO> listData = msFseWarnService.ruleList(deptId, type);
        return ResultUtil.success(listData);
    }


    @PostMapping("/user/add")
    public Result userAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @Validated @RequestBody FseWarnSetUserParam request) {
        logger.info("设置特种设备报警接收人及接收方式,loginer:{},param:{}", user.getUsername(), request.toString());
        msFseWarnService.userAdd(request);
        return ResultUtil.success();
    }

    @GetMapping("/record/list")
    public Result recordList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @RequestParam("deptId") Integer deptId,
                             @RequestParam("pageNum") Integer pageNum,
                             @RequestParam("pageSize") Integer pageSize,
                             @RequestParam("state") Integer state,
                             @RequestParam(value = "type", required = false) Integer type,
                             @RequestParam(value = "ruleType", required = false) Integer ruleType,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(value = "startDate", required = false) Date startDate,
                             @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam(value = "endDate", required = false) Date endDate) {
        logger.info("特种设备报警记录,loginer:{},deptId:{},pageNum:{},pageSize:{},state:{},ruleType:{},type:{},startDate:{},endDate:{}",
                user.getUsername(), deptId, pageNum, pageSize, state, ruleType, type, startDate, endDate);
        Date startTime = null;
        Date endTime = null;
        if (startDate != null) {
            startTime = DateUtil.getDateBegin(startDate);
        }
        if (endDate != null) {
            endTime = DateUtil.getDateEnd(endDate);
        }
        PageData<FseWarnRecordDTO> pageData = msFseWarnService.recordList(deptId, pageNum, pageSize, state, ruleType, type, startTime, endTime);
        return ResultUtil.success(pageData);
    }

    @PostMapping("/record/handle")
    public Result recordHandle(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @Validated @RequestBody AppWarnHandleParam request) {
        logger.info("处理特种设备报警,loginer:{},param:{}", user.getUsername(), request.toString());
        msFseWarnService.recordHandle(user, request);
        return ResultUtil.success();
    }

    @PostMapping("/record/batchHandle")
    public Result batchHandle(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                              @Validated @RequestBody WarnBatchHandleParam param) {
        logger.info("批量处理特种设备报警,loginer:{},param:{}", user.getUsername(), param.toString());
        param.setUserId(user.getId());
        param.setUserName(user.getNickname());
        param.setPhone(user.getPhone());
        msFseWarnService.batchHandle(param);
        return ResultUtil.success();
    }

    @PostMapping("/record/del")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                      @Validated @RequestBody WarnBatchDelParam param) {
        logger.info("批量删除特种设备报警,loginer:{},param:{}", user.getUsername(), param.toString());
        msFseWarnService.del(param);
        return ResultUtil.success();
    }

    @GetMapping("/record/detail")
    public Result recordDetail(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                               @RequestParam("id") Integer id) {
        logger.info("特种设备的报警详情,loginer:{},id:{}", user.getUsername(), id);
        FseWarnDetailDTO fseWarnDetailDTO = msFseWarnService.recordDetail(id);
        return ResultUtil.success(fseWarnDetailDTO);
    }

    @GetMapping("/crane/count")
    public Result craneWarnCount(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                                 @RequestParam("craneId") Integer craneId) {
        logger.info("塔机当日报警记录统计，loginer:{},craneId:{}", user.getUsername(), craneId);
        FseCraneWarnCountDTO fseCraneWarnCountDTO = msFseWarnService.countCraneWarn(craneId);
        return ResultUtil.success(fseCraneWarnCountDTO);
    }


}
